package priv.mill.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.model.User;

//项目/模块路径/方法路径

@Controller
@RequestMapping("/hello")
public class HelloController {
	
	
//	@RequestMapping(value="/{addr}/info",method=RequestMethod.POST,params= {"name","age!=10"},headers= {"User-Agent"})//  /hello/info
	@GetMapping(value="/{addr}/info",headers= {"User-Agent"})
	@ResponseBody
	public String info( @PathVariable("addr") String abc,
			@RequestHeader("User-Agent") String ua) {
		return "<h1>这里是一个独立的Controller文件:"+ua+"</h1>";
	}
	
	
	@GetMapping("testcook")
	public @ResponseBody String cook(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		String cv="";
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("Hm_lvt_8875c662941dbf07e39c556c8d97615f")) {
				cv=cookie.getValue();
			}
		}
		return cv;
	}
	
	@GetMapping("testcook2")
	public @ResponseBody String cook2(
			@CookieValue("Hm_lvt_8875c662941dbf07e39c556c8d97615f") String sv)
	{
		return sv;
	}
	
	@GetMapping("login")
	public @ResponseBody Object login(User user,Map<String, Object> map) {
		user.setUserName("劉德華");
		map.put("user", user);
		return user;
	}
}
